package com.jlkf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.TblCourseMapper;
import com.jlkf.dao.TblOrderMapper;
import com.jlkf.pojo.TblCourse;
import com.jlkf.pojo.TblOrder;
import com.jlkf.service.CourseService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Service
public class CourseServiceImpl implements CourseService{

	@Autowired
	private TblCourseMapper tblCourseMapper;
	
	@Autowired
	private TblOrderMapper tblOrderMapper;
	
	
	@Override
	public ReturnData myCourseInfo(Long userId, Long orderId, Integer type, Integer status, String orderNo,
			Long goodsId,Integer pageSize, Integer pageNum,Integer moneyStatus) {
		if(pageNum==null){
			pageNum=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		 if(userId!=null){
			 map.put("userId", userId);
		 }
		 if(orderId!=null){
			 map.put("orderId", orderId);
		 }
		 if(type!=null){
			 map.put("type", type);
		 }
		 if(status!=null){
			 map.put("status", status);
		 }
		 if(!StringTools.isEmpty(orderNo)){
			 map.put("orderNo", orderNo);
		 }
		 if(goodsId!=null){
			 map.put("goodsId", goodsId);
		 }
		 if(moneyStatus!=null){
			 map.put("moneyStatus", moneyStatus);
		 }
		 List<TblOrder> myCourseInfo = tblOrderMapper.myCourseInfo(map);
		 for(int i = 0;i<myCourseInfo.size();i++){
			 TblOrder order = myCourseInfo.get(i);
			 if(order.getgOrderType()==1){
				 TblCourse course = tblCourseMapper.selectByPrimaryKey((long)order.getgCourseId());
				 if(course!=null){
					 order.setCourseName(course.getgCourseName());
				 }
			 }
			 if(order.getgOrderType()==2){
				 if(order.getgOrderStatus()==0){
					 myCourseInfo.remove(i);
					 i--;
				 }
			 }
		 } 
		 PageHelper.startPage(pageNum, pageSize);  
		 List<TblOrder> list = myCourseInfo;
 		 PageInfo<TblOrder> info = new PageInfo<>(list); 
		 return new ReturnData(list, info.getPages(),(int)info.getTotal(),200, "订单信息");
	}


	@Override
	public ReturnData deleteOrCancel(Long gId, Integer status) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("gId", gId);
		map.put("status", status);
 		int ret = tblOrderMapper.updateStatus(map);
 		if(ret>0){
 			return new ReturnData(200,"操作成功");
 		}else{
 		    return new ReturnData(500,"操作失败");
 		}
	}

	
	
	
}
